package com.example.administrator.xiaoxiang.fragment.Rebatefragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.activity.DetailPagesActivity;
import com.example.administrator.xiaoxiang.adapter.BaseAdapter;
import com.example.administrator.xiaoxiang.adapter.RebateAdapter;
import com.example.administrator.xiaoxiang.data.RebateDate;
import com.example.administrator.xiaoxiang.url.URLResource;
import com.example.administrator.xiaoxiang.utils.OkHttpHelper;
import com.example.administrator.xiaoxiang.utils.SpacesItemDecoration;
import com.example.administrator.xiaoxiang.utils.SpotsCallBack;
import com.google.gson.Gson;
import com.squareup.okhttp.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotCommoditiesFragment extends Fragment {
    private List<RebateDate.BodyBean.RebateGoodsBean> mRebateGoodsBeanList;
    private OkHttpHelper mOkHttpHelper=OkHttpHelper.getInstance();
    private RebateAdapter mRebateAdapter;
    private RecyclerView mRecyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_hot_commodities, container, false);
        mRecyclerView= (RecyclerView) view.findViewById(R.id.recycler_HotCommodities);
        init();
        return view;
    }

    private void init() {

        mOkHttpHelper.get(URLResource.RebateURL, new SpotsCallBack(getContext()) {
            @Override
            public void onSuccess(String response) {

                Gson gson=new Gson();
                RebateDate rebateDate=gson.fromJson(response,RebateDate.class);
                mRebateGoodsBeanList=new ArrayList<RebateDate.BodyBean.RebateGoodsBean>();
                mRebateGoodsBeanList.addAll(rebateDate.getBody().getRebateGoods());
                mRebateAdapter = new RebateAdapter(getContext(), mRebateGoodsBeanList);
                mRecyclerView.setAdapter(mRebateAdapter);
                mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
                if(isAdded()){
                   int leftRight = dip2px(7);
                   int topBottom = dip2px(7);
                    mRecyclerView.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
                }

                mRebateAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                    @Override
                    public void OnClick(View view, int position) {
                        Intent intent=new Intent(getContext(), DetailPagesActivity.class);
                        intent.putExtra("getGoodsCode",mRebateAdapter.getItem(position).getGoodsCode()+"");
                        Log.e("FFF","FFF"+mRebateAdapter.getItem(position).getGoodsCode());
                        startActivity(intent);


                    }
                });
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });

    }


    public int dip2px(float dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics());
    }
}
